﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CrossGenLogic
{
    public class CSVReader
    {
        private int width, height;

        public int Width
        {
            get
            {
                return width;
            }
        }

        public int Height
        {
            get
            {
                return height;
            }
        }

        public List<VocElement> readvoc(string path)
        {
            List<VocElement> voc = new List<VocElement>();
            try
            {
                using (StreamReader readFile = new StreamReader(path, System.Text.Encoding.Default, false))
                {
                    string line;
                    string[] row;
                    int diff;
                    char[] charSeparators = new char[] {',', ' '};
                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(charSeparators, 3, StringSplitOptions.RemoveEmptyEntries);
                        diff = Convert.ToInt32(row[1]);
                        VocElement newword = new VocElement(row[0], diff, row[2]);
                        voc.Add(newword);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message+" в чтении словаря");
            }

            return voc;
        }
        public List<GridElement> readgrid(string path)
        {
            List<GridElement> grid = new List<GridElement>();
            try
            {
                using (StreamReader readFile = new StreamReader(path, System.Text.Encoding.Default, false))
                {
                    string line;
                    string[] row;
                    int x, y, length;
                    bool dir;
                    char[] charSeparators = new char[] { ',', ' ' };
                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(charSeparators, 4, StringSplitOptions.RemoveEmptyEntries);
                        if (row.Length == 2)
                        {
                            this.width = Convert.ToInt32(row[0]);
                            this.height = Convert.ToInt32(row[1]);
                        }
                        else
                        {
                            x = Convert.ToInt32(row[0]);
                            y = Convert.ToInt32(row[1]);
                            length = Convert.ToInt32(row[2]);
                            if (row[3] == "h") dir = true;
                            else dir = false;
                            GridElement newelem = new GridElement(x, y, dir, length);
                            grid.Add(newelem);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message+" в чтении сетки");
            }

            return grid;
        }
    }
}
